﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DevComponents.DotNetBar;

namespace QLST
{
    public partial class frmPhanQuyen : Form
    {
        PhanQuyenBUS cls = new PhanQuyenBUS();
        PhanQuyenDTO p = new PhanQuyenDTO();
        Boolean flag;

        public frmPhanQuyen()
        {
            InitializeComponent();
        }

        private void frmPhanQuyen_Load(object sender, EventArgs e)
        {
            ClearTextBox();
            LockTextBox(false);
            LockButton(true);
            LoadPQ();
        }

        public void LoadPQ()
        {
            gvPhanQuyen.DataSource = cls.Load_PhanQuyen();
        }
        private void ClearGV()
        {
            gvPhanQuyen.ClearSelection();
        }

        private void ClearTextBox()
        {
             txtTenDangNhap.Text  = "";
            txtMatKhau.Text  = "";
            
        }

        private void LockButton(Boolean f)
        {
            btnNVThem.Enabled = f;
            btnNVSua.Enabled = f;
            btnNVXoa.Enabled = f;
            btnNVLuu.Enabled = !f;
            btnNVHuy.Enabled = !f;
        }

        private void LockTextBox(Boolean f)
        {
             txtTenDangNhap.Enabled = f;
            txtMatKhau.Enabled = f;
            cmbMaNND.Enabled = f;
            cmbMaNV.Enabled = f;
            cmbMaQuyen.Enabled = f;
            
            if (f == false)
            {
                txtTenDangNhap.BackColor = Color.LemonChiffon;
                txtMatKhau.BackColor = Color.LemonChiffon;
                cmbMaNND.BackColor = Color.LemonChiffon;
                cmbMaNV.BackColor = Color.LemonChiffon;
                cmbMaQuyen.BackColor = Color.LemonChiffon;
                }
            else if (f == true)
            {
                
                txtTenDangNhap.BackColor = Color.White;
                txtMatKhau.BackColor = Color.White;
              
                cmbMaNND.BackColor = Color.White;
                cmbMaNV.BackColor = Color.White;
                cmbMaQuyen.BackColor = Color.White;
                
            }
        }

        private void btnNVThem_Click(object sender, EventArgs e)
        {
            LockButton(false);
            LockTextBox(true);
            ClearTextBox();
            gvPhanQuyen.Enabled = false;
            cmbMaQuyen.Focus();
            cmbMaQuyen.DataSource = cls.MQ();
            cmbMaQuyen.DisplayMember = "MaQuyen";
            cmbMaQuyen.ValueMember = "MaQuyen";
            cmbMaNV.DataSource = cls.MNV();
            cmbMaNV.DisplayMember = "MaNV";
            cmbMaNV.ValueMember = "MaNV";
            cmbMaNND.DataSource = cls.MNND();
            cmbMaNND.DisplayMember = "MaNhomNguoiDung";
            cmbMaNND.ValueMember = "MaNhomNguoiDung";
            
        }

        private void gvPhanQuyen_SelectionChanged(object sender, EventArgs e)
        {
            txtTenDangNhap.Text = gvPhanQuyen.CurrentRow.Cells["colTenDangNhap"].Value.ToString();
            txtMatKhau.Text = gvPhanQuyen.CurrentRow.Cells["colMatKhau"].Value.ToString();
            cmbMaNV.Text = gvPhanQuyen.CurrentRow.Cells["colMaNV"].Value.ToString();
            cmbMaQuyen.Text = gvPhanQuyen.CurrentRow.Cells["colMaQuyen"].Value.ToString();
            cmbMaNND.Text = gvPhanQuyen.CurrentRow.Cells["colMaNND"].Value.ToString();
           
        }

        private void btnNVXoa_Click(object sender, EventArgs e)
        {
            p.MaQuyen = cmbMaQuyen.Text;
            p.MaNhom = cmbMaNND.Text;

            cls.Del_PhanQuyen(p);
            frmPhanQuyen_Load (sender, e);
        }

        private void btnNVSua_Click(object sender, EventArgs e)
        {
            LockTextBox(true);
            LockButton(false);
            gvPhanQuyen.Enabled = false;
            cmbMaQuyen.Focus();
        }

        private void btnNVLuu_Click(object sender, EventArgs e)
        {

            if (cmbMaQuyen.Text == "")
            {
                flag = true;
            }
            else if (cmbMaQuyen.Text != "")
            {
                flag = false;
            }
            p.MaQuyen = cmbMaQuyen.Text;
            p.MaNhom  = cmbMaNND.Text;
            p.MaNV = cmbMaNV.Text;
            p.TenDangNhap  = txtTenDangNhap .Text ;
            p.MatKhau = txtMatKhau.Text;
            cls.IsUp_PhanQuyen(p, flag);
            ClearGV();
            frmPhanQuyen_Load (sender, e);
            gvPhanQuyen.Enabled = true;
            MessageBoxEx.Show("Cập nhật thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void btnNVHuy_Click(object sender, EventArgs e)
        {
            ClearGV();
            
            frmPhanQuyen_Load (sender, e);
            LockButton(true);
            LockTextBox(false);
            gvPhanQuyen.Enabled = true;
        }

        
        
    }
}
